$(() => {


    //二次封装ajax
    let myAjax = function (type, url, obj1, success) {


        let contentType = 'application/x-www-form-urlencoded'
        let processData = true
        if (obj1 instanceof FormData) {
            contentType = false
            processData = false
        }
        $.ajax({

            type,
            url: 'http://localhost:8080/api/v1' + url,
            data: obj1,
            headers: {
                Authorization: localStorage.getItem('bigNews')
            },
            contentType,
            processData,
            success


        })

    }

    //定义一个对象发请求的时候用
    let obj1 = {
        page: 1,
        perpage: 10
    }
    //渲染分页
    let fenYe = function (count) {
        layui.use('laypage', function () {
            var laypage = layui.laypage;

            //执行一个laypage实例
            laypage.render({
                elem: 'fenye',//注意，这里的 test1 是 ID，不用加 # 号
                count,
                limit: obj1.perpage,
                curr: obj1.page,

                jump: function (obj, first) {
                    // //obj包含了当前分页的所有参数
                    if (!first) {
                        //do something
                        obj1.page = obj.curr

                        //改变对象里面的page请求页数,然后重新渲染
                        getCommenList()

                    }
                } //数据总数，从服务端得到
            })
        })
    }
    //请求参数渲染页面
    let getCommenList = function () {

        myAjax('get', '/admin/comment/search', obj1, (res) => {
            // console.log(res);
            let htmlStr = template('commen_list', res)
            $('tbody').html(htmlStr)


            //请求完数据再渲染分页
            fenYe(res.data.totalCount)
        })

    }
    getCommenList()


    //事件委托批准
    $('tbody').on('click', '.btn_pizhun', function () {

        let id = $(this).data('id')
        layer.confirm('是否批准?', { icon: 3, title: '提示' }, function (index) {
            //do something
            myAjax('post', '/admin/comment/pass', { id }, (res) => {

                getCommenList()
            })
            layer.close(index);
        });


    })


    //事件委托拒绝
    $('tbody').on('click', '.btn_jujue', function () {

        let id = $(this).data('id')
        layer.confirm('是否拒绝?', { icon: 3, title: '提示' }, function (index) {
            //do something
            myAjax('post', '/admin/comment/reject', { id }, (res) => {

                getCommenList()
            })
            layer.close(index);
        });


    })


    //事件委托删除
    $('tbody').on('click', '.btn_shanchu', function () {

        let id = $(this).data('id')
        layer.confirm('是否删除?', { icon: 3, title: '提示' }, function (index) {
            //do something
            myAjax('post', '/admin/comment/delete', { id }, (res) => {

                getCommenList()
            })
            layer.close(index);
        });


    })
})
